/*************************************************
Description:智能家居--统计模块的实现
            温度统计、湿度统计、烟雾浓度统计
**************************************************/
#include "statisticwidget.h"
#include "ui_statisticwidget.h"
#include  "control.h"
#include <QDateTime>
#include <QTime>
#include <QTimer>
#include <QDate>


//界面上要绘制的点数
#define MAX_POINT_NUM   18

//纵坐标最大值
#define PLOT_TEMPTURE_MAX_Y         50     //温度值
#define PLOT_HUMIDITY_MAX_Y         100     //湿度值
#define PLOT_SMOKE_MAX_Y            30   //烟雾浓度值

#define TextColor QColor(255,255,0)         //黄色
#define Plot_NoColor QColor(0,0,0,0)        //黑色

#define Plot1_DotColor QColor(5,189,251)
#define Plot1_LineColor QColor(41,138,220)
#define Plot1_BGColor QColor(41,138,220,80)

#define Plot2_DotColor QColor(236,110,0)
#define Plot2_LineColor QColor(246,98,0)
#define Plot2_BGColor QColor(246,98,0,80)

#define Plot3_DotColor QColor(204,0,0)
#define Plot3_LineColor QColor(246,0,0,200)
#define Plot3_BGColor QColor(246,98,0,80)

#define Plot1_Count 12
#define Plot2_Count 10

#define Plot1_MaxY 50
#define Plot2_MaxY 100
#define Plot3_MaxY 500
#define Plot4_MaxY 100

#define TextWidth 1
#define LineWidth 1.2
#define DotWidth  3

#ifdef _ARM_
#define Plot5_MaxY 20
#else
#define Plot5_MaxY 50
#endif


StatisticWidget::StatisticWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StatisticWidget)
{
    ui->setupUi(this);

    initForm();
    initPlot();
    initPlotTempture();
    initPlotHumidity();
    initPlotSmoke();

    connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));

}

StatisticWidget::~StatisticWidget()
{
    delete ui;
}

void StatisticWidget::initForm()
{
    m_currentIndex = 0;

    m_plots_list.append(ui->plot_tempture);
    m_plots_list.append(ui->plot_humidity);
    m_plots_list.append(ui->plot_smoke);

    //默认显示图例
#if SHOW_LEGEND
    m_plots_list.at(0)->legend->setVisible(true);
    m_plots_list.at(1)->legend->setVisible(true);
    m_plots_list.at(2)->legend->setVisible(true);
    ui->ckText->setChecked(true);
#endif

    ui->label_dateTime->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
}

//初始化绘图设备
void StatisticWidget::initPlot()
{
    //设置各个图的纵坐标名称
    m_plots_list.at(0)->yAxis->setLabel(tr("温度值(单位:°)"));
    m_plots_list.at(1)->yAxis->setLabel(tr("湿度值(单位:%)"));
    m_plots_list.at(2)->yAxis->setLabel(tr("浓度值(单位:%)"));

    m_plots_list.at(0)->xAxis->setLabel(tr("时间"));
    m_plots_list.at(1)->xAxis->setLabel(tr("时间"));
    m_plots_list.at(2)->xAxis->setLabel(tr("时间"));

    //设置纵坐标范围
    m_plots_list.at(0)->yAxis->setRange(0,PLOT_TEMPTURE_MAX_Y);
    m_plots_list.at(1)->yAxis->setRange(0,PLOT_HUMIDITY_MAX_Y);
    m_plots_list.at(2)->yAxis->setRange(0,PLOT_SMOKE_MAX_Y);

    foreach (QCustomPlot *plot, m_plots_list) {
        //设置坐标颜色，名称
        plot->yAxis->setLabelColor(TextColor);
        plot->xAxis->setLabelColor(TextColor);

        plot->yAxis->setTickLabelColor(TextColor);  //Y轴标签颜色
        plot->xAxis->setTickLabelColor(TextColor);

        //设置标签字体
        plot->xAxis->setTickLabelFont(QFont(QFont().family(),10));

        plot->xAxis->setBasePen(QPen(TextColor,TextWidth));
        plot->yAxis->setBasePen(QPen(TextColor,TextWidth));

        plot->xAxis->setTickPen(QPen(TextColor,TextWidth));
        plot->yAxis->setTickPen(QPen(TextColor,TextWidth));

        plot->xAxis->setSubTickPen(QPen(TextColor,TextWidth));
        plot->yAxis->setSubTickPen(QPen(TextColor,TextWidth));

        //设置画布背景
        QLinearGradient plotGradient;
        plotGradient.setStart(0,0);
        plotGradient.setFinalStop(0,350);
        plotGradient.setColorAt(0,QColor(80,80,80));
        plotGradient.setColorAt(1,QColor(50,50,50));
        plot->setBackground(plotGradient);

        //设置坐标背景色
        QLinearGradient axisRectGradient;
        axisRectGradient.setStart(0,0);
        axisRectGradient.setFinalStop(0,350);
        axisRectGradient.setColorAt(0,QColor(80,80,80));
        axisRectGradient.setColorAt(1,QColor(30,30,30));
        plot->axisRect()->setBackground(axisRectGradient);

        //设置图例提示位置以及背景色
        plot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
        plot->legend->setBrush(QColor(255, 255, 255, 200));

        plot->replot();
    }
}

//初始化温度统计图
void StatisticWidget::initPlotTempture()
{
    //向画布添加一条曲线
    m_plots_list.at(0)->addGraph();//客厅温度
    m_plots_list.at(0)->graph(0)->setName(tr("温度值(单位:℃)"));
    m_plots_list.at(0)->graph(0)->setPen(QPen(Plot1_LineColor,LineWidth));

    m_plots_list.at(0)->graph(0)->setScatterStyle(
                QCPScatterStyle(QCPScatterStyle::ssCircle,
                                QPen(Plot1_DotColor, LineWidth),
                                QBrush(Plot1_DotColor), DotWidth));
//    m_plots_list.at(0)->addGraph();//卧室温度
//    m_plots_list.at(0)->graph(1)->setName(tr("卧室温度值(单位:度)"));
//    m_plots_list.at(0)->graph(1)->setPen(QPen(Plot3_LineColor,LineWidth));
//    m_plots_list.at(0)->graph(1)->setScatterStyle(
//                QCPScatterStyle(QCPScatterStyle::ssCircle,
//                                QPen(Plot3_DotColor, LineWidth),
//                                QBrush(Plot3_DotColor), DotWidth));


    m_plots_list.at(0)->xAxis->setAutoTickStep(false);
    m_plots_list.at(0)->xAxis->setTickStep(2);
    m_plots_list.at(0)->xAxis->setRange(0, 12,Qt::AlignLeft);

    m_plots_list.at(0)->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    m_plots_list.at(0)->xAxis->setDateTimeFormat("hh:mm:ss");
    m_plots_list.at(0)->axisRect()->setupFullAxesBox();
    m_plots_list.at(0)->replot();




}

//初始化湿度统计图
void StatisticWidget::initPlotHumidity()
{
    //向画布添加一条曲线
    m_plots_list.at(1)->addGraph(0);
    m_plots_list.at(1)->graph(0)->setName(tr("湿度值(单位:%)"));
    m_plots_list.at(1)->graph(0)->setPen(QPen(Plot2_LineColor,LineWidth));
    m_plots_list.at(1)->graph(0)->setScatterStyle(
                QCPScatterStyle(QCPScatterStyle::ssCircle,
                                QPen(Plot2_DotColor, LineWidth),
                                QBrush(Plot2_DotColor), DotWidth));

    m_plots_list.at(1)->xAxis->setAutoTickStep(false);
    m_plots_list.at(1)->xAxis->setTickStep(2);
    m_plots_list.at(1)->xAxis->setRange(0, 12, Qt::AlignLeft);
    m_plots_list.at(1)->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    m_plots_list.at(1)->xAxis->setDateTimeFormat("hh:mm:ss");
    m_plots_list.at(1)->axisRect()->setupFullAxesBox();
    m_plots_list.at(1)->replot();
}

//初始化烟雾统计图
void StatisticWidget::initPlotSmoke()
{
    m_plots_list.at(2)->addGraph(0);
    m_plots_list.at(2)->graph(0)->setName(tr("烟雾值(单位:%)"));
    m_plots_list.at(2)->graph(0)->setPen(QPen(Plot3_LineColor,LineWidth));
    m_plots_list.at(2)->graph(0)->setScatterStyle(
                QCPScatterStyle(QCPScatterStyle::ssCircle,
                                QPen(Plot3_DotColor, LineWidth),
                                QBrush(Plot3_DotColor), DotWidth));

    m_plots_list.at(2)->xAxis->setAutoTickStep(false);
    m_plots_list.at(2)->xAxis->setTickStep(2);
    m_plots_list.at(2)->xAxis->setRange(0, 12, Qt::AlignLeft);

    m_plots_list.at(2)->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    m_plots_list.at(2)->xAxis->setDateTimeFormat("hh:mm:ss");
    m_plots_list.at(2)->axisRect()->setupFullAxesBox();
    m_plots_list.at(2)->replot();
}


void StatisticWidget::initConnect()
{
//    connect(ui->comboBox_view_data,SIGNAL(currentIndexChanged(int)),
//            this,SLOT(slotChangeData(int)));
}

void StatisticWidget::realtimeDataSlot()
{

    double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;//毫秒-秒
    qsrand(QTime::currentTime().msec() + QTime::currentTime().second() * 10000);

    double value = qrand() %5+20;
    //温度显示
    ui->plot_tempture->graph(0)->addData(key,value);
    ui->plot_tempture->graph(0)->removeDataBefore(key-12);
    ui->plot_tempture->graph(0)->rescaleValueAxis();

//    ui->plot_tempture->graph(1)->addData(key,value+3);
//    ui->plot_tempture->graph(1)->removeDataBefore(key-12);
//    ui->plot_tempture->graph(1)->rescaleValueAxis();

    ui->plot_tempture->xAxis->setRange(key+0.75, 12, Qt::AlignRight);//重设x轴的范围
    m_plots_list.at(0)->yAxis->setRange(0,PLOT_TEMPTURE_MAX_Y);//重设y轴的范围
    ui->plot_tempture->replot();

    //湿度显示
    ui->plot_humidity->graph(0)->addData(key,value+35);
    ui->plot_humidity->graph(0)->removeDataBefore(key-12);
    ui->plot_humidity->graph(0)->rescaleValueAxis();
    ui->plot_humidity->xAxis->setRange(key+0.75, 12, Qt::AlignRight);//重设x轴的范围
    m_plots_list.at(1)->yAxis->setRange(0,PLOT_HUMIDITY_MAX_Y );//重设y轴的范围
    ui->plot_humidity->replot();

    //烟雾显示
    ui->plot_smoke->graph(0)->addData(key,value-20);
    ui->plot_smoke->graph(0)->removeDataBefore(key-12);
    ui->plot_smoke->graph(0)->rescaleValueAxis();
    ui->plot_smoke->xAxis->setRange(key+0.75, 12, Qt::AlignRight);//重设x轴的范围
    m_plots_list.at(2)->yAxis->setRange(0,PLOT_TEMPTURE_MAX_Y);//重设y轴的范围
    ui->plot_smoke->replot();


}





void StatisticWidget::on_checkBox_toggled(bool checked)
{
    if(ui->comboBox_view_data->currentText()=="实时数据")
    {
        if(checked==true)
            dataTimer.start(1000);
        else
            dataTimer.stop();
    }
}

void StatisticWidget::on_ckText_toggled(bool checked)
{

    if(checked==true)
    {
        m_plots_list.at(0)->legend->setVisible(true);
        m_plots_list.at(1)->legend->setVisible(true);
        m_plots_list.at(2)->legend->setVisible(true);
    }
    else
    {
        m_plots_list.at(0)->legend->setVisible(false);
        m_plots_list.at(1)->legend->setVisible(false);
        m_plots_list.at(2)->legend->setVisible(false);
    }

    m_plots_list.at(0)->replot();
    m_plots_list.at(1)->replot();
    m_plots_list.at(2)->replot();

}
